// ==UserScript==
// @name           	Message Frame Alerts
// @namespace      	falcons.marknas.com
// @author			Carolus Rex, 2011
// @description    	Adds cluster alerts to message bar
// @include        	http://orion.pardus.at/msgframe.php*
// @version		   	1.1.0
// ==/UserScript==

//////////////////////////////////////////////////////////////
// GLOBAL USER SETTINGS
//////////////////////////////////////////////////////////////
// By default, the script will NOT show alerts from enemy clusters
// [i.e. clusters normally owned by Empire/Union. This can be changed
// using the variables below.
// 
// Say you want to see alerts from PEC. Locate this line below:
// 
// var showEmpireClusters = false;
// 
// ... and achange it to:
// 
// var showEmpireClusters = true;
//////////////////////////////////////////////////////////////
var showEmpireClusters = true;
var showUnionClusters = false;

function getValidationCode(){
	if ((localStorage.getItem('alertValidation') == null) || (localStorage.getItem('alertValidation').length < 20)){
		var valid = prompt('Enter validation code: ', '');
		localStorage.setItem('alertValidation', valid);
		return valid;
	}
	else return localStorage.getItem('alertValidation');
}

function setNewValidationCode(){
	var currentCode = "";
	if (localStorage.getItem('alertValidation') != null){
		currentCode = localStorage.getItem('alertValidation');
	}
	var valid = prompt('Enter validation code: ', currentCode);
	localStorage.setItem('alertValidation', valid);
}

function getAlerts(addy) {
	GM_xmlhttpRequest({
	  method: "GET",
	  url: addy,
	  onload: function(response) {
		displayAlerts(response.responseText);
	  }
	});
}

function checkOnline(pilot){
	if (localStorage.getItem('onlinePilots') != null){
		var onlinePilots = JSON.parse(localStorage.getItem('onlinePilots')).onlinePilots;
		for(var i = 0; i < onlinePilots.length; i++){
			if(onlinePilots[i].pilot == pilot){
				return pilot + "*";
			}
		}
	}
	return pilot;
}

function displayAlerts(alerts){
	var alarmHTML = document.createElement("div");
	if (alerts.indexOf('|') != -1) {
		var tds = document.getElementsByTagName("td");
		var msg_al = tds[tds.length-1];
		// Federal clusters:
		var frcAlarmCount = 0;
		var frcSectors = "";
		var fhcAlarmCount = 0;
		var fhcSectors = "";
		var shcAlarmCount = 0;
		var shcSectors = "";
		var pfcAlarmCount = 0;
		var pfcSectors = "";
		// Pardus Core
		var pcAlarmCount = 0;
		var pcSectors = "";
		// Neutral Zones:
		var wprAlarmCount = 0;
		var wprSectors = "";
		var nprAlarmCount = 0;
		var nprSectors = "";
		var eprAlarmCount = 0;
		var eprSectors = "";
		var sprAlarmCount = 0;
		var sprSectors = "";
		var laneAlarmCount = 0;
		var laneSectors = "";
		var splialarmHTMLCount = 0;
		var splitSectors = "";
		var gapAlarmCount = 0;
		var gapSectors = "";
		// Imperial clusters:
		var pecAlarmCount = 0;
		var pecSectors = "";
		var ekcAlarmCount = 0;
		var ekcSectors = "";
		var escAlarmCount = 0;
		var escSectors = "";
		var ewsAlarmCount = 0;
		var ewsSectors = "";
		// Union clusters:
		var pucAlarmCount = 0;
		var pucSectors = "";
		var ukcAlarmCount = 0;
		var ukcSectors = "";
		var urcAlarmCount = 0;
		var urcSectors = "";
		var unrAlarmCount = 0;
		var unrSectors = "";
				
		alarmHTML.innerHTML = "";
		alarms = alerts.split(";");
		
		//Find the clusters where alerts have been reported, and count them:
		for (var i=0; i<alarms.length; i++){
			var data = alarms[i].split("|");
			var cluster = data[0];
			var attacker = checkOnline(data[4]);
			var defender = checkOnline(data[5]);
			if (cluster == "FRC") {
				frcAlarmCount++;
				frcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "FHC") {
				fhcAlarmCount++;
				fhcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "FSH") {
				shcAlarmCount++;
				shcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "PFC") {
				pfcAlarmCount++;
				pfcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "PC") {
				pcAlarmCount++;
				pcSectors += attacker + " " + data[3] + " " + defender + ", Pardus Core " + data[2] + " || ";
			}
			else if (cluster == "WPR") {
				wprAlarmCount++;
				wprSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "NPR") {
				nprAlarmCount++;
				nprSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "EPR") {
				eprAlarmCount++;
				eprSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "SPR") {
				sprAlarmCount++;
				sprSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "LANE") {
				laneAlarmCount++;
				laneSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "SPLIT") {
				splialarmHTMLCount++;
				splitSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			else if (cluster == "GAP") {
				gapAlarmCount++;
				gapSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
			}
			//Empire:
			else if (showEmpireClusters == true){
				if (cluster == "PEC") {
					pecAlarmCount++;
					pecSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "EKC") {
					ekcAlarmCount++;
					ekcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "ESC") {
					escAlarmCount++;
					escSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "EWS") {
					ewsAlarmCount++;
					ewsSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
			}
			// Union:
			else if (showUnionClusters == true){
				if (cluster == "PUC") {
					pucAlarmCount++;
					pucSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "UKC") {
					ukcAlarmCount++;
					ukcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "URC") {
					urcAlarmCount++;
					urcSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
				else if (cluster == "UNR") {
					unrAlarmCount++;
					unrSectors += attacker + " " + data[3] + " " + defender + ", " + data[1] + " " + data[2] + " || ";
				}
			}
		}
		
		//Build the actual links:
		if (frcAlarmCount > 0) {
			frcSectors = frcSectors.substr(0, frcSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=frc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/FRC.png" border="0" title="' + frcSectors + '" alt="FRC Alert!"><span id="new_alert"><b>' + frcAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (fhcAlarmCount > 0) {
			fhcSectors = fhcSectors.substr(0, fhcSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=fhc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/FHC.png" border="0" title="' + fhcSectors + '" alt="FHC Alert!"><span id="new_alert"><b>' + fhcAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (shcAlarmCount > 0) {
			shcSectors = shcSectors.substr(0, shcSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=fsh" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/SHC.png" border="0" title="' + shcSectors + '" alt="SHC Alert!"><span id="new_alert"><b>' + shcAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (pfcAlarmCount > 0) {
			pfcSectors = pfcSectors.substr(0, pfcSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=pfc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/PFC.png" border="0" title="' + pfcSectors + '" alt="PFC Alert!"><span id="new_alert"><b>' + pfcAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (pcAlarmCount > 0) {
			pcSectors = pcSectors.substr(0, pcSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=pc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/PC.png" border="0" title="' + pcSectors + '" alt="PC Alert!"><span id="new_alert"><b>' + pcAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (wprAlarmCount > 0) {
			wprSectors = wprSectors.substr(0, wprSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=wpr" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/WPR.png" border="0" title="' + wprSectors + '" alt="WPR Alert!"><span id="new_alert"><b>' + wprAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (nprAlarmCount > 0) {
			nprSectors = nprSectors.substr(0, nprSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=npr" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/NPR.png" border="0" title="' + nprSectors + '" alt="NPR Alert!"><span id="new_alert"><b>' + nprAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (eprAlarmCount > 0) {
			eprSectors = eprSectors.substr(0, eprSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=epr" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/EPR.png" border="0" title="' + eprSectors + '" alt="EPR Alert!"><span id="new_alert"><b>' + eprAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (sprAlarmCount > 0) {
			sprSectors = sprSectors.substr(0, sprSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=spr" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/SPR.png" border="0" title="' + sprSectors + '" alt="SPR Alert!"><span id="new_alert"><b>' + sprAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (laneAlarmCount > 0) {
			laneSectors = laneSectors.substr(0, laneSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=lane" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/LANE.png" border="0" title="' + laneSectors + '" alt="LANE Alert!"><span id="new_alert"><b>' + laneAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		if (splialarmHTMLCount > 0) {
			splitSectors = splitSectors.substr(0, splitSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=split" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/SPLIT.png" border="0" title="' + splitSectors + '" alt="SPLIT Alert!"><span id="new_alert"><b>' + splialarmHTMLCount + '</b></span></a>&nbsp;</span>';
		}
		if (gapAlarmCount > 0) {
			gapSectors = gapSectors.substr(0, gapSectors.length-3);
			alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=gap" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/GAP.png" border="0" title="' + gapSectors + '" alt="GAP Alert!"><span id="new_alert"><b>' + gapAlarmCount + '</b></span></a>&nbsp;</span>';
		}
		// Empire:
		if (showEmpireClusters == true) {
			if (pecAlarmCount > 0) {
				pecSectors = pecSectors.substr(0, pecSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=pec" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/PEC.png" border="0" title="' + pecSectors + '" alt="PEC Alert!"><span id="new_alert"><b>' + pecAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (ekcAlarmCount > 0) {
				ekcSectors = ekcSectors.substr(0, ekcSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=ekc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/EKC.png" border="0" title="' + ekcSectors + '" alt="EKC Alert!"><span id="new_alert"><b>' + ekcAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (escAlarmCount > 0) {
				escSectors = escSectors.substr(0, escSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=esc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/ESC.png" border="0" title="' + escSectors + '" alt="ESC Alert!"><span id="new_alert"><b>' + escAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (ewsAlarmCount > 0) {
				ewsSectors = ewsSectors.substr(0, ewsSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=ews" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/EWS.png" border="0" title="' + ewsSectors + '" alt="EWS Alert!"><span id="new_alert"><b>' + ewsAlarmCount + '</b></span></a>&nbsp;</span>';
			}
		}
		// Union:
		if (showUnionClusters == true) {
			if (pucAlarmCount > 0) {
				pucSectors = pucSectors.substr(0, pucSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=puc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/PUC.png" border="0" title="' + pucSectors + '" alt="PUC Alert!"><span id="new_alert"><b>' + pucAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (ukcAlarmCount > 0) {
				ukcSectors = ukcSectors.substr(0, ukcSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=ukc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/UKC.png" border="0" title="' + ukcSectors + '" alt="UKC Alert!"><span id="new_alert"><b>' + ukcAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (urcAlarmCount > 0) {
				urcSectors = urcSectors.substr(0, urcSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=urc" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/URC.png" border="0" title="' + urcSectors + '" alt="URC Alert!"><span id="new_alert"><b>' + urcAlarmCount + '</b></span></a>&nbsp;</span>';
			}
			if (unrAlarmCount > 0) {
				unrSectors = unrSectors.substr(0, unrSectors.length-3);
				alarmHTML.innerHTML += '<span class="nobreak"><a href="http://logger.fedfalcons.com/alerts.php?cluster=unr" target="_blank"><img src="http://logger.fedfalcons.com/alerter/img/UNR.png" border="0" title="' + unrSectors + '" alt="UNR Alert!"><span id="new_alert"><b>' + unrAlarmCount + '</b></span></a>&nbsp;</span>';
			}
		}
	}
	else if (alerts.indexOf('Invalid user') != -1) {
		alert("Your failed login has been logged.");
		setNewValidationCode();
	}
	
	//Add the alert(s) to the message frame:
	msg_al.appendChild(alarmHTML);

}

var user = document.getElementById('universe').getAttribute('title');
user = user.substring(7);
var validation = getValidationCode();
var address = "http://logger.fedfalcons.com/alerter/getalerts.php?uname="+user+"&vcode="+validation;

getAlerts(address);